#include "stdafx.h"
#include "LinkLabelExample.h"

#include "hwinapplication.h"
#include "hwinform.h"
#include "hwinmenu.h"
#include "hwinstdctrls.h"
#include <iostream>

#include <Shellapi.h>

#pragma comment(linker,"\"/manifestdependency:type='win32' \
name='Microsoft.Windows.Common-Controls' version='6.0.0.0' \
processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")

using namespace harlinn::windows;

class LinkLabelForm : public Form
{
    std::shared_ptr<LinkLabel> linkLabel;
public:
    typedef Form Base;
    LinkLabelForm();
protected:
    virtual void DoOnInitialize();
    virtual void DoOnSize(Message& message);
    virtual void DoOnPaint(Message& message);
};

LinkLabelForm::LinkLabelForm()
    : Base()
{
    SetText(L"LinkLabel example");
    SetSize(300,70);
}

void LinkLabelForm::DoOnInitialize()
{
    Base::DoOnInitialize(); 
    auto self = As<LinkLabelForm>();
    linkLabel = make_control<LinkLabel>(self);
    linkLabel->SetText(L"<A HREF=\"http://harlinn.com\">harlinn.COM</A>");
    linkLabel->OnClick.connect([&](LinkLabel* sender,NMLINK* pNMLINK)
    {
        ShellExecute(NULL, L"open", pNMLINK->item.szUrl, NULL, NULL, SW_SHOW);
    });
    
}

void LinkLabelForm::DoOnSize(Message& message)
{
    harlinn::windows::Rectangle clientRect = GetClientRect();
    linkLabel->MoveWindow(5,5,clientRect.Width() - 10,21);
}

void LinkLabelForm::DoOnPaint(Message& message)
{
    Base::DoOnPaint(message);
    auto dc = std::make_shared<PaintDeviceContextHandle>(As<Control>());
    dc->DrawDesktopWallPaper();
}

int APIENTRY _tWinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPTSTR lpCmdLine,int nCmdShow)
{
	UNREFERENCED_PARAMETER(hPrevInstance);
	UNREFERENCED_PARAMETER(lpCmdLine);
    try
    {
        auto application = make_component<Application>();
        auto form = make_control<LinkLabelForm>();
        
        auto result = application->Run(form);

        return result;
    }
    catch(std::exception& exc)
    {
        std::cout << exc.what() << std::endl;
    }
    catch(...)
    {
        std::cout << "Unknown exception" << std::endl;    
    }
    return 0;
}